'''
Created on 5 nov 2012

@author: Lanboost
'''
import Image

class CBufferedImage:
    data = []
    width = -1
    height = -1
    format = ""
    mode = ""
    
    '''
        Constructor
        TAKES: Image
    '''
    def __init__(self, image):
        pixels = image.getdata()
        self.data = []
        for pixel in pixels:
            self.data.append(pixel)
        self.width = image.size[0]
        self.height = image.size[1]
        self.format = image.format
        self.mode = image.mode
        
    def getData(self):
        return self.data
    def getWidth(self):
        return self.width
    
    def getHeight(self):
        return self.height
    
    def getPixel(self, x, y):
        return self.data[y*self.getWidth()+x]
    
    def setPixel(self, x,y, rgb):
        if not isinstance(rgb, tuple):
            print "RGB is not tuple"
        #Now check if is in format of RGB pr RGBA
        if self.mode == "RGB":
            #Pixel is in RGBA format, convert it to right format
            if len(rgb) == 4:
                rgb = (rgb[0], rgb[1], rgb[2])
            if len(rgb) != 3:
                #pixel isn't in right format
                print "RGB isn't in mode RGB"
            self.data[y*self.getWidth()+x] = rgb
                
    '''
    GIVES: Image - The buffered image as a Image
    '''
    def convertToImage(self):
        image = Image.new(self.mode, (self.getWidth(), self.getHeight()))
        image.putdata(self.getData())
        return image

